package com.hakim.wfb.customer.service.biz;

import com.alibaba.fastjson.JSON;
import com.hakim.wfb.bean.CreditPoint;
import com.hakim.wfb.bean.PointOrder;
import com.hakim.wfb.bean.User;
import com.hakim.wfb.biz.CreditPointBiz;
import com.hakim.wfb.biz.PointOrderBiz;
import com.hakim.wfb.biz.UserBiz;
import com.hakim.wfb.common.enums.PointTypeEnum;
import com.hakim.wfb.common.enums.TransTypeEnum;
import com.hakim.wfb.common.serial.SerialNoGenerator;
import com.hakim.wfb.common.serial.UUIDGenerator;
import com.hakim.wfb.common.util.DateUtils;
import com.hakim.wfb.common.util.EnumUtils;
import com.hakim.wfb.customer.constant.Constants;
import com.hakim.wfb.customer.enums.ErrorCodeEnum;
import com.hakim.wfb.customer.params.trans.CalculatePointExchangeParameter;
import com.hakim.wfb.customer.params.trans.ExchangeCreditPointParameter;
import com.hakim.wfb.customer.params.trans.GetTransPointParameter;
import com.hakim.wfb.customer.params.wallet.GetCoinMarketPriceParam;
import com.hakim.wfb.customer.result.trans.PointExchangeCalculateView;
import com.hakim.wfb.customer.result.wallet.CoinMarketPriceResult;
import com.hakim.wfb.customer.result.wallet.JiuFuCoinPriceResult;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.exception.HkfsBizException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service("transPointBizService")
public class TransPointBizService {

    private static final String DEFAULT_ORG_SPILT = "_";//组织机构分隔符

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserBiz userBiz;

    @Autowired
    private CreditPointBiz creditPointBiz;

    @Autowired
    private PointOrderBiz pointOrderBiz;

    /**
     * 积分转账
     */
    @Transactional
    public ResultEx createTransOrder(GetTransPointParameter param) {

        Date now = new Date();
        String nowStr = DateUtils.dateToStrLong(now) + " ";

        ResultEx resultEx = new ResultEx();

        if (param.getTransPoint() <= 0) {
            logger.warn(nowStr + "转账额度错误");
            return resultEx.makeFailedResult("转账额度错误");
        }
        //判断
        User fromUser = userBiz.getUser(param.getSessionIdentity().getUuid());
        if (fromUser == null) {
            logger.warn(nowStr + "用户id：" + param.getSessionIdentity().getUserId() + ",无此用户信息");
            return resultEx.makeFailedResult("无此用户积分信息");
        }

        if (param.getPayPwd().equals(fromUser.getUserTradePwd()) == false) {
            logger.warn(nowStr + "用户id：" + param.getSessionIdentity().getUuid() + ",支付密码错误");
            return resultEx.makeFailedResult("支付密码错误");
        }

        CreditPoint fromCreditPoint = creditPointBiz
                .getCreditPointById(param.getSessionIdentity().getUuid());
        if (fromCreditPoint == null) {
            logger.warn(nowStr + "用户id：" + param.getSessionIdentity().getUuid() + ",无此用户积分信息");
            return resultEx.makeFailedResult("无此用户积分信息");
        }

        //判断
        if (StringUtils.isEmpty(param.getUserCode())) {
            logger.warn(nowStr + "用户：" + param.getUserCode() + ",无此用户信息");
            return resultEx.makeFailedResult("无此用户信息");
        }

        User toUser = userBiz.getUserByUserCode(param.getUserCode());
        if (toUser == null) {
            logger.warn(nowStr + "用户：" + param.getUserCode() + ",无此用户信息");
            return resultEx.makeFailedResult("无此用户信息");
        }

        CreditPoint toCreditPoint = creditPointBiz.getCreditPointById(toUser.getTid());
        if (toCreditPoint == null) {
            logger.warn(nowStr + "用户id：" + toUser.getTid() + ",无此用户积分信息");
            return resultEx.makeFailedResult("无此用户积分信息");
        }

        Boolean isDirct = false;
        List<User> listDirectOrign = userBiz
                .getUsersByUserOrg(fromUser.getUserOrg() + DEFAULT_ORG_SPILT);
        for (User u : listDirectOrign) {
            if (u.getTid().equals(toUser.getTid())) {
                isDirct = true;
                break;
            }
        }

        if (param.getPointType() != PointTypeEnum.ELEC_POINT.getValue()) {
            if (isDirct == false) {
                return resultEx.makeFailedResult("非下线用户，不许转账");
            }
        }

        Class<PointTypeEnum> clazz = PointTypeEnum.class;
        String des = (String) EnumUtils.getEnumDescriotionByValue(param.getPointType(), clazz);
        String orderNo = SerialNoGenerator.getTransPointOrderNo(fromUser.getUserPhone());

        //相应积分类型减少
        if (param.getPointType() == PointTypeEnum.ACTIVITY_POINT.getValue()) {
            if (fromCreditPoint.getCpActivitPoint() < param.getTransPoint()) {
                return resultEx.makeFailedResult("积分不足！");
            }

            fromCreditPoint
                    .setCpActivitPoint(fromCreditPoint.getCpActivitPoint() - param.getTransPoint());
            toCreditPoint
                    .setCpActivitPoint(toCreditPoint.getCpActivitPoint() + param.getTransPoint());
        } else if (param.getPointType() == PointTypeEnum.CUST_POINT.getValue()) {
            if (fromCreditPoint.getCpCustPoint() < param.getTransPoint()) {
                return resultEx.makeFailedResult("积分不足！");
            }
            fromCreditPoint
                    .setCpCustPoint(fromCreditPoint.getCpCustPoint() - param.getTransPoint());
            toCreditPoint.setCpCustPoint(toCreditPoint.getCpCustPoint() + param.getTransPoint());
        } else if (param.getPointType() == PointTypeEnum.ELEC_POINT.getValue()) {
            if (fromCreditPoint.getCpElecPoint() < param.getTransPoint()) {
                return resultEx.makeFailedResult("积分不足！");
            }
            fromCreditPoint
                    .setCpElecPoint(fromCreditPoint.getCpElecPoint() - param.getTransPoint());
            toCreditPoint.setCpElecPoint(toCreditPoint.getCpElecPoint() + param.getTransPoint());
        }

        try {

            int updateCount = creditPointBiz.update(fromCreditPoint);
            if (updateCount <= 0) {
                throw new HkfsBizException(ErrorCodeEnum.CONCURRENT_MODIFY.getCode(),
                        ErrorCodeEnum.CONCURRENT_MODIFY.getMessage());
            }
            updateCount = creditPointBiz.update(toCreditPoint);
            if (updateCount <= 0) {
                throw new HkfsBizException(ErrorCodeEnum.CONCURRENT_MODIFY.getCode(),
                        ErrorCodeEnum.CONCURRENT_MODIFY.getMessage());
            }

            //积分减少
            PointOrder reversePointOrder = new PointOrder();
            reversePointOrder.setTid(UUIDGenerator.getUUID());

            if (param.getPointType() == PointTypeEnum.ACTIVITY_POINT.getValue()) {
                reversePointOrder.setPoAmount(fromCreditPoint.getCpActivitPoint());
            } else if (param.getPointType() == PointTypeEnum.CUST_POINT.getValue()) {
                reversePointOrder.setPoAmount(fromCreditPoint.getCpCustPoint());
            } else if (param.getPointType() == PointTypeEnum.ELEC_POINT.getValue()) {
                reversePointOrder.setPoAmount(fromCreditPoint.getCpElecPoint());
            }

            reversePointOrder.setPoCreateDate(DateUtils.getNow());
            reversePointOrder.setPoDescription(
                    "转账" + toUser.getUserCode() + "：" + des + "-" + param.getTransPoint());
            reversePointOrder.setPoOrderno(orderNo);
            reversePointOrder.setPoFromUser(toUser.getTid());
            reversePointOrder.setPoToUser(fromUser.getTid());
            reversePointOrder.setPoPointType(param.getPointType());
            reversePointOrder.setPoTransPoint(param.getTransPoint() * -1);
            reversePointOrder.setPoType(TransTypeEnum.TRANSFER.getValue());
            pointOrderBiz.add(reversePointOrder);

            //积分增加
            PointOrder pointOrder = new PointOrder();
            pointOrder.setTid(UUIDGenerator.getUUID());
            if (param.getPointType() == PointTypeEnum.ACTIVITY_POINT.getValue()) {
                pointOrder.setPoAmount(toCreditPoint.getCpActivitPoint());
            } else if (param.getPointType() == PointTypeEnum.CUST_POINT.getValue()) {
                pointOrder.setPoAmount(toCreditPoint.getCpCustPoint());
            } else if (param.getPointType() == PointTypeEnum.ELEC_POINT.getValue()) {
                pointOrder.setPoAmount(toCreditPoint.getCpElecPoint());
            }
            pointOrder.setPoCreateDate(DateUtils.getNow());
            pointOrder.setPoDescription("转账：" + des + "" + param.getTransPoint());
            pointOrder.setPoOrderno(orderNo);
            pointOrder.setPoPointType(param.getPointType());
            pointOrder.setPoFromUser(fromUser.getTid());
            pointOrder.setPoToUser(toUser.getTid());
            pointOrder.setPoTransPoint(param.getTransPoint());
            pointOrder.setPoType(TransTypeEnum.TRANSFER.getValue());
            pointOrderBiz.add(pointOrder);

            logger.info(nowStr + fromUser.getUserCode() + "转账" + toUser.getUserCode() + "：" + des
                    + param.getTransPoint() + "，积分变动订单号！【" + orderNo + "】");

            return resultEx.makeSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            return resultEx.makeFailedResult("系统错误！");
        }
    }

    /**
     * 积分兑换
     */
    @Transactional
    public ResultEx exchangePoint(ExchangeCreditPointParameter parameter) {

        ResultEx resultEx = new ResultEx();
        Date now = new Date();
        String nowStr = DateUtils.dateToStrLong(now) + " ";
        String userId = parameter.getSessionIdentity().getUuid();
        //判断
        User user = userBiz.getUser(userId);
        if (user == null) {
            logger.warn(nowStr + "用户id：" + userId + ",无此用户信息");
            resultEx.setRetCode(ErrorCodeEnum.USER_NOT_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        if (!parameter.getTradePassword().equals(user.getUserTradePwd())) {
            logger.warn(nowStr + "用户id：" + userId + ",支付密码错误");
            resultEx.setRetCode(ErrorCodeEnum.USER_TRADE_PASSWORD_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.USER_TRADE_PASSWORD_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        CreditPoint creditPoint = creditPointBiz.getCreditPointById(userId);
        if (creditPoint == null) {
            logger.warn(nowStr + "用户id：" + userId + ",无此用户积分信息");
            resultEx.setRetCode(ErrorCodeEnum.USER_CREDIT_POINT_NOT_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.USER_CREDIT_POINT_NOT_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }

        //计算兑换后的积分
        CalculatePointExchangeParameter calculateParameter = new CalculatePointExchangeParameter();
        calculateParameter.setSourcePointType(parameter.getSourcePointType());
        calculateParameter.setSourcePointAmount(parameter.getSourcePointAmount());
        calculateParameter.setTargetPointType(parameter.getTargetPointType());
        ObjectResultEx<PointExchangeCalculateView> pointExchangeViewResult = this
                .calculateExchangePoint(calculateParameter);
        if (pointExchangeViewResult.isFailed()) {
            resultEx.setRetCode(pointExchangeViewResult.getRetCode());
            resultEx.setRetMsg(pointExchangeViewResult.getRetMsg());
            return resultEx.makeFailedResult();
        }

        PointExchangeCalculateView pointExchangeView = pointExchangeViewResult.getDataObject();
        creditPoint.setTid(creditPoint.getTid());

        PointTypeEnum sourcePointType = PointTypeEnum.getByValue(parameter.getSourcePointType());
        PointTypeEnum targetPointType = PointTypeEnum.getByValue(parameter.getTargetPointType());

        if (sourcePointType.getValue() == PointTypeEnum.STATIC_POINT.getValue()) {
//            if (creditPoint.getCpStaticPoint() < parameter.getSourcePointAmount()) {
//                return resultEx.makeFailedResult("积分不足！");
//            }
            if (creditPoint.getCpAvailableStaticPoint() < parameter.getSourcePointAmount()) {
                resultEx.setRetCode(ErrorCodeEnum.CREDIT_POINT_BALANCE_NOT_ENOUGH_ERROR.getCode());
                resultEx.setRetMsg(
                        ErrorCodeEnum.CREDIT_POINT_BALANCE_NOT_ENOUGH_ERROR.getMessage());
                return resultEx.makeFailedResult();
            }
            creditPoint.setCpStaticPoint(
                    creditPoint.getCpStaticPoint() - parameter.getSourcePointAmount());
            creditPoint.setCpAvailableStaticPoint(
                    creditPoint.getCpAvailableStaticPoint() - parameter.getSourcePointAmount());
            creditPoint.setCpElecPoint(
                    creditPoint.getCpElecPoint() + pointExchangeView.getPointAmount());
            creditPoint.setCpReusablePoint(
                    creditPoint.getCpReusablePoint() + pointExchangeView.getPointReusable());

        } else if (sourcePointType.getValue() == PointTypeEnum.DYNAMIC_POINT.getValue()) {
            if (creditPoint.getCpDynamicPoint() < parameter.getSourcePointAmount()) {
                resultEx.setRetCode(ErrorCodeEnum.CREDIT_POINT_BALANCE_NOT_ENOUGH_ERROR.getCode());
                resultEx.setRetMsg(
                        ErrorCodeEnum.CREDIT_POINT_BALANCE_NOT_ENOUGH_ERROR.getMessage());
                return resultEx.makeFailedResult();
            }
            if (parameter.getSourcePointAmount() % 100 != 0
                    || parameter.getSourcePointAmount() < 500) {
                return resultEx.makeFailedResult("动态积分100的倍数，500元起提！");
            }
            creditPoint.setCpDynamicPoint(
                    creditPoint.getCpDynamicPoint() - parameter.getSourcePointAmount());
            creditPoint.setCpElecPoint(
                    creditPoint.getCpElecPoint() + pointExchangeView.getPointAmount());
            creditPoint.setCpReusablePoint(
                    creditPoint.getCpReusablePoint() + pointExchangeView.getPointReusable());
        }

        String des = (String) EnumUtils
                .getEnumDescriotionByValue(sourcePointType, PointTypeEnum.class);
        String orderNo = SerialNoGenerator.getExchargeOrderNo(user.getUserPhone());

        try {

            int updateCount = creditPointBiz.update(creditPoint);
            if (updateCount <= 0) {
                throw new HkfsBizException(ErrorCodeEnum.CONCURRENT_MODIFY.getCode(),
                        ErrorCodeEnum.CONCURRENT_MODIFY.getMessage());
            }

            //增加积分变动流水记录 该类积分为负数
            PointOrder reversePointOrder = new PointOrder();
            reversePointOrder.setTid(UUIDGenerator.getUUID());
            reversePointOrder.setPoCreateDate(DateUtils.getNow());
            reversePointOrder.setPoDescription(
                    sourcePointType.getDescription() + "兑换：" + des + "-" + parameter
                            .getSourcePointAmount());
            reversePointOrder.setPoFromUser(userId);
            reversePointOrder.setPoToUser(userId);
            reversePointOrder.setPoOrderno(orderNo);
            reversePointOrder.setPoPointType(sourcePointType.getValue());
            reversePointOrder.setPoTransPoint(parameter.getSourcePointAmount() * -1);
            if (sourcePointType.getValue() == PointTypeEnum.STATIC_POINT.getValue()) {
                reversePointOrder.setPoAmount(creditPoint.getCpStaticPoint());
            } else if (sourcePointType.getValue() == PointTypeEnum.DYNAMIC_POINT.getValue()) {
                reversePointOrder.setPoAmount(creditPoint.getCpDynamicPoint());
            }
            reversePointOrder.setPoType(TransTypeEnum.EXCHANGE.getValue());
            pointOrderBiz.add(reversePointOrder);
            logger.info(nowStr + user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                    + "，积分变动订单号！【" + orderNo + "】");

            //增加积分变动流水记录 该类积分为正数
            PointOrder plusPointOrder = new PointOrder();
            plusPointOrder.setTid(UUIDGenerator.getUUID());

            plusPointOrder.setPoCreateDate(DateUtils.getNow());
            plusPointOrder.setPoDescription(
                    sourcePointType.getDescription() + "兑换：" + des + "+" + pointExchangeView
                            .getPointAmount());
            plusPointOrder.setPoFromUser(userId);
            plusPointOrder.setPoToUser(userId);
            plusPointOrder.setPoOrderno(orderNo);
            plusPointOrder.setPoPointType(targetPointType.getValue());
            if (targetPointType.getValue() == PointTypeEnum.ELEC_POINT.getValue()) {
                plusPointOrder.setPoAmount(creditPoint.getCpElecPoint());
            }
            plusPointOrder.setPoTransPoint(pointExchangeView.getPointAmount() * 1);
            plusPointOrder.setPoType(TransTypeEnum.EXCHANGE.getValue());
            pointOrderBiz.add(plusPointOrder);

            //复消积分增加
            PointOrder reusablePointOrder = new PointOrder();
            reusablePointOrder.setTid(UUIDGenerator.getUUID());

            reusablePointOrder.setPoCreateDate(DateUtils.getNow());
            reusablePointOrder.setPoDescription(
                    "兑换：" + des + parameter.getSourcePointAmount() + "，增加：" + pointExchangeView
                            .getPointReusable());
            reusablePointOrder.setPoFromUser(userId);
            reusablePointOrder.setPoToUser(userId);
            reusablePointOrder.setPoOrderno(orderNo);
            reusablePointOrder.setPoPointType(PointTypeEnum.REUSABLE_POINT.getValue());
            reusablePointOrder.setPoTransPoint(pointExchangeView.getPointReusable());
            reusablePointOrder.setPoAmount(creditPoint.getCpReusablePoint());
            reusablePointOrder.setPoType(TransTypeEnum.EXCHANGE.getValue());
            pointOrderBiz.add(reusablePointOrder);
            logger.info(nowStr + user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                    + "，复消积分增加" + pointExchangeView.getPointReusable() + "。积分变动订单号！【" + orderNo
                    + "】");

            //积分管理费增加
            if (pointExchangeView.getPointFee() > 0) {
                PointOrder feePointOrder = new PointOrder();
                feePointOrder.setTid(UUIDGenerator.getUUID());
                feePointOrder.setPoAmount(0);
                feePointOrder.setPoCreateDate(DateUtils.getNow());
                feePointOrder.setPoDescription(
                        user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                                + "，系统手续费增加" + pointExchangeView.getPointFee());
                feePointOrder.setPoFromUser(user.getTid());
                feePointOrder.setPoOrderno(orderNo);
                feePointOrder.setPoPointType(PointTypeEnum.FEE_POINT.getValue());
                feePointOrder.setPoToUser("0");
                feePointOrder.setPoTransPoint(pointExchangeView.getPointFee());
                feePointOrder.setPoType(TransTypeEnum.EXCHANGE.getValue());
                pointOrderBiz.add(feePointOrder);
                logger.info(
                        nowStr + user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                                + "，系统手续费增加" + pointExchangeView.getPointFee() + "。积分变动订单号！【"
                                + orderNo + "】");
            }

            //公益基金系统增加
            PointOrder foundationPointOrder = new PointOrder();
            foundationPointOrder.setTid(UUIDGenerator.getUUID());
            foundationPointOrder.setPoAmount(0);
            foundationPointOrder.setPoCreateDate(DateUtils.getNow());
            foundationPointOrder.setPoDescription(
                    user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                            + "，系统公益基金增加" + pointExchangeView.getPointFoundation());
            foundationPointOrder.setPoFromUser(user.getTid());
            foundationPointOrder.setPoOrderno(orderNo);
            foundationPointOrder.setPoPointType(PointTypeEnum.FOUNDATION_POINT.getValue());
            foundationPointOrder.setPoToUser("0");
            foundationPointOrder.setPoTransPoint(pointExchangeView.getPointFoundation());
            foundationPointOrder.setPoType(TransTypeEnum.EXCHANGE.getValue());
            pointOrderBiz.add(foundationPointOrder);
            logger.info(nowStr + user.getUserCode() + "兑换：" + des + parameter.getSourcePointAmount()
                    + "，系统公益基金增加" + pointExchangeView.getPointFoundation() + "。积分变动订单号！【" + orderNo
                    + "】");

            return resultEx.makeSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            return resultEx.makeFailedResult("系统错误！");
        }
    }

    /**
     * 计算积分兑换
     */
    public ObjectResultEx<PointExchangeCalculateView> calculateExchangePoint(
            CalculatePointExchangeParameter
                    parameter) {

        ObjectResultEx<PointExchangeCalculateView> resultEx = new ObjectResultEx();
        PointTypeEnum sourcePointType = PointTypeEnum.getByValue(parameter.getSourcePointType());
        if (sourcePointType == null) {

            resultEx.makeFailedResult("积分类型无效");
        }
        PointTypeEnum targetPointType = PointTypeEnum.getByValue(parameter.getTargetPointType());
        if (targetPointType == null) {
            resultEx.makeFailedResult("积分类型无效");
        }
        PointExchangeCalculateView pointExchangeCalculateView = new PointExchangeCalculateView();
        //静态积分
        if (sourcePointType.getValue() == PointTypeEnum.STATIC_POINT.getValue()
                || sourcePointType.getValue() == PointTypeEnum.DYNAMIC_POINT.getValue()) {

            if (targetPointType.getValue() != PointTypeEnum.ELEC_POINT.getValue()) {
                resultEx.makeFailedResult("不支持" + sourcePointType.getDescription() + "兑换为" +
                        targetPointType.getDescription());
            }

            pointExchangeCalculateView.setPointAmount(parameter.getSourcePointAmount() * 0.7);
            pointExchangeCalculateView.setPointReusable(parameter.getSourcePointAmount() * 0.20);
            pointExchangeCalculateView.setPointFee(parameter.getSourcePointAmount() * 0.05);
            pointExchangeCalculateView.setPointFoundation(parameter.getSourcePointAmount() * 0.05);

            resultEx.setDataObject(pointExchangeCalculateView);
            return resultEx.makeSuccessResult();

//        } else if (parameter.getSourcePointType() == PointTypeEnum.DYNAMIC_POINT.getValue()) {
//            //动态积分
//            if (!parameter.getTargetPointType().equals(PointTypeEnum.ELEC_POINT.getValue())) {
//                resultEx.makeFailedResult("不支持" + PointTypeEnum.STATIC_POINT.getDescription() + "兑换为" +
//                        PointTypeEnum.getByValue(parameter.getTargetPointType()).getDescription());
//            }
//            pointExchangeCalculateView.setPointAmount(parameter.getSourcePointAmount() * 0.8);
//            pointExchangeCalculateView.setPointRepeat(parameter.getSourcePointAmount() * 0.15);
//            pointExchangeCalculateView.setPointManager(parameter.getSourcePointAmount() * 0.05);
//            pointExchangeCalculateView.setPointBenefical(parameter.getSourcePointAmount() * 0.05);
        } else {
            resultEx.setRetMsg("不支持" + sourcePointType.getDescription() + "兑换");
            return resultEx.makeFailedResult();
        }
    }

    public ObjectResultEx<CoinMarketPriceResult> getCoinCurrentPrice(
            GetCoinMarketPriceParam param) {

        ObjectResultEx<CoinMarketPriceResult> resultObjectResultEx = new ObjectResultEx<>();
        if (StringUtils.isBlank(Constants.BLOCKCHAIN_COIN_MARKET_PRICE_URL)) {
            resultObjectResultEx
                    .setRetCode(ErrorCodeEnum.WALLET_MARKET_PRICE_URL_NOT_EXIST_ERROR.getCode());
            resultObjectResultEx
                    .setRetMsg(ErrorCodeEnum.WALLET_MARKET_PRICE_URL_NOT_EXIST_ERROR.getMessage());
            return resultObjectResultEx.makeFailedResult();
        }
        try {
            String resultXml = Request.Get(Constants.BLOCKCHAIN_COIN_MARKET_PRICE_URL)
                    .execute().returnContent().asString();
            logger.info("久福币价格请求响应结果：{}", resultXml);
            if (StringUtils.isNotBlank(resultXml)) {

                JiuFuCoinPriceResult jiuFuCoinPriceResult = JSON
                        .parseObject(URLDecoder.decode(resultXml, "UTF-8"),
                                JiuFuCoinPriceResult.class);
                if (jiuFuCoinPriceResult != null && jiuFuCoinPriceResult.getJfcCny() != null &&
                        jiuFuCoinPriceResult.getJfcCny().length >= 10) {

                    CoinMarketPriceResult coinMarketPriceResult = new CoinMarketPriceResult();
                    coinMarketPriceResult.setCoinCode(jiuFuCoinPriceResult.getJfcCny()[8]);
                    coinMarketPriceResult.setCoinName(jiuFuCoinPriceResult.getJfcCny()[0]);
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[1])) {
                        coinMarketPriceResult.setCurrentPrice(
                                new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[1]));
                    }
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[2])) {
                        coinMarketPriceResult
                                .setBuyPrice(new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[2]));
                    }
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[3])) {
                        coinMarketPriceResult
                                .setSellPrice(new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[3]));
                    }
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[4])) {
                        coinMarketPriceResult
                                .setDealAmount(new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[4]));
                    }
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[6])) {
                        coinMarketPriceResult
                                .setDealCount(new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[6]));
                    }
                    if (StringUtils.isNotBlank(jiuFuCoinPriceResult.getJfcCny()[7])) {
                        coinMarketPriceResult
                                .setUpAndDown(new BigDecimal(jiuFuCoinPriceResult.getJfcCny()[7]));
                    }
                    resultObjectResultEx.setDataObject(coinMarketPriceResult);
                    return resultObjectResultEx.makeSuccessResult();
                }
            }
            return resultObjectResultEx.makeFailedResult("请求久福币价格接口异常");
        } catch (Exception e) {
            logger.error("请求久福币价格接口异常", e);
            return resultObjectResultEx.makeFailedResult("请求久福币价格接口异常");
        }
    }
}
